package com.cy.utils;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.ICallback;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by daiyemanyou on 16/2/5.
 */
public class WorkShipICallback   {
    public String beiZf=null;
    public String zf=null;
    public String beiZfPhone=null;
    public ResultSet rs=null;
   /* @Override
    public Object run(Connection conn) throws SQLException {
        CallableStatement proc=null;
        try {
            proc=(CallableStatement) conn.prepareCall("{call bless(?, ?, ?) }");
            proc.setString(1, beiZf);
            proc.setString(2, zf);
            proc.setString(3, beiZfPhone);
            proc.execute();
            ResultSet rs =proc.getResultSet();
            while (rs.next()) {
                System.out.print(rs.getInt(1));
                System.out.print(rs.getInt(2));
            }
//            System.out.print(rs.getInt("stat_owner"));
        } finally {
            DbKit.close(proc, conn);
        }
        return rs;
    }*/

    public JSONObject fetchAgentId(final String beiZf ,final String zf ,final  String beiZfPhone){
        JSONObject result = (JSONObject) Db.execute(new ICallback() {
            @Override
            public Object run(Connection conn) throws SQLException {
                CallableStatement cs = null;
                cs = conn.prepareCall("{call bless(?,?,?)}");
                cs.setString(1, beiZf);
                cs.setString(2, zf);
                cs.setString(3, beiZfPhone);
                cs.execute();
                ResultSet rs = cs.getResultSet(); //To change body of implemented methods use File | Settings | File Templates.

                JSONObject jsonObject = new JSONObject();
               /* jsonObject.put("agentId", cs.getInt(4));
                jsonObject.put("code", cs.getInt(5));
                jsonObject.put("msg", cs.getString(6));*/
                while (rs.next()) {
//                    System.out.print(rs.getInt(1));
//                    System.out.print(rs.getInt(2));
                    jsonObject.put("stat_owner", rs.getInt(1));
                    jsonObject.put("stat_all", rs.getString(2));
                }
                return jsonObject;
            }
        });
        return result;
    }

    public JSONObject countBless(){
        JSONObject result = (JSONObject) Db.execute(new ICallback() {
            @Override
            public Object run(Connection conn) throws SQLException {
                CallableStatement cs = null;
                cs = conn.prepareCall("{call allBlessCount()}");
                cs.execute();
                ResultSet rs = cs.getResultSet(); //To change body of implemented methods use File | Settings | File Templates.

                JSONObject jsonObject = new JSONObject();
               /* jsonObject.put("agentId", cs.getInt(4));
                jsonObject.put("code", cs.getInt(5));
                jsonObject.put("msg", cs.getString(6));*/
                while (rs.next()) {
//                    System.out.print(rs.getInt(1));
//                    System.out.print(rs.getInt(2));
                    jsonObject.put("countBless", rs.getInt(1));
                }
                return jsonObject;
            }
        });
        return result;
    }

}